﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.model;
using l1cs.server.serverpackets;

namespace l1cs.server.clientpackets
{
    public class C_PickUpItem : ClientBasePacket
    {
        private const String _C_PICK_UP_ITEM = "[C] C_PickUpItem";
        private static Logger _log = Logger.getLogger( typeof( C_PickUpItem ).FullName );

        public C_PickUpItem( byte[] decrypt, ClientThread client )
            : base( decrypt )
        {
            int x = readH();
            int y = readH();
            int objectId = readD();
            int pickupCount = readD();

            L1PcInstance pc = client.getActiveChar();
            if ( pc.isDead() || pc.isGhost() )
            {
                return;
            }

            if ( pc.isInvisble() )
            { // インビジ状態
                return;
            }
            if ( pc.isInvisDelay() )
            { // インビジディレイ状態
                return;
            }

            L1Inventory groundInventory = L1World.getInstance().getInventory( x, y,
                    pc.getMapId() );
            L1Object l1object = groundInventory.getItem( objectId );

            if ( l1object != null && !pc.isDead() )
            {
                L1ItemInstance item = (L1ItemInstance)l1object;
                if ( item.getItemOwnerId() != 0
                        && pc.getId() != item.getItemOwnerId() )
                {
                    pc.sendPackets( new S_ServerMessage( 623 ) ); // アイテムが拾えませんでした。
                    return;
                }
                if ( pc.getLocation().getTileLineDistance( item.getLocation() ) > 3 )
                {
                    return;
                }

                if ( item.getItem().getItemId() == L1ItemId.ADENA )
                {
                    L1ItemInstance inventoryItem = pc.getInventory().findItemId(
                            L1ItemId.ADENA );
                    int inventoryItemCount = 0;
                    if ( inventoryItem != null )
                    {
                        inventoryItemCount = inventoryItem.getCount();
                    }
                    // 拾った後に2Gを超過しないようにチェック
                    if ( (long)inventoryItemCount + (long)pickupCount > 2000000000L )
                    {
                        pc.sendPackets( new S_ServerMessage( 166, // \f1%0が%4%1%3%2
                                "所持しているアデナ", "2,000,000,000を超過するので拾えません。" ) );
                        return;
                    }
                }

                if ( pc.getInventory().checkAddItem( // 容量重量確認及びメッセージ送信
                        item, pickupCount ) == L1Inventory.OK )
                {
                    if ( item.getX() != 0 && item.getY() != 0 )
                    { // ワールドマップ上のアイテム
                        groundInventory.tradeItem( item, pickupCount, pc
                                .getInventory() );
                        pc.turnOnOffLight();

                        pc.sendPackets( new S_AttackPacket( pc, objectId,
                                ActionCodes.ACTION_Pickup ) );
                        if ( !pc.isGmInvis() )
                        {
                            pc.broadcastPacket( new S_AttackPacket( pc, objectId,
                                    ActionCodes.ACTION_Pickup ) );
                        }
                    }
                }
            }
        }

        override
        public String getType()
        {
            return _C_PICK_UP_ITEM;
        }
    }
}